﻿<?php

class User_Models_User {

	public function __construct() {

		include_once APPLICATION_PATH . '/../public/u/config.inc.php';
		include_once APPLICATION_PATH . '/../public/u/uc_client/client.php';
	}

	/**
	 * 获取头像
	 */
	public function getAvatar($size = 'middle' , $uid = 0 , $type = 'virtual') {

		$uid = $uid ? $uid : $this->getUserId ();
		return UC_API . '/avatar.php?uid=' . $uid . '&type=' . $type . '&size=' . $size;
	}

	/**
	 * 设置头像
	 */
	public function setAvatar() {

		return uc_avatar ( $this->getUserId () );
	}

	/**
	 * 获取uid
	 */
	public function getUserId() {

		if ($_COOKIE ['user']) {
			$user = Zend_Json::decode ( $_COOKIE ['user'] );
			//$user = json_decode($_COOKIE ['user'] );
			return $user ['uid'];
		}
		else {
			return 0;
		}
	}

	/**
	 * 获取用户名
	 */
	public function getUserName($uid = '') {

		if ($uid) {
			$data = uc_get_user ( $uid, 1 );
			$user ['uid'] = $data [0];
			$user ['username'] = $data [1];
			$user ['email'] = $data [2];
		}
		else {
			$user = Zend_Json::decode ( $_COOKIE ['user'] );
		}
		return $user ['username'];
	}

	/**
	 * 消息中心
	 */
	public function getMessageCenter() {

		return uc_pm_location ( $this->getUserId () );
	}

	/**
	 * 获取新的消息
	 */
	public function getMewMsg() {

		return uc_pm_checknew ( $this->getUserId (), 2 );
	}

	/**
	 * 登入
	 */
	public function login($username , $password , $isuid = 0 , $time = '') {

		$data = uc_user_login ( $username, $password, $isuid );
		$user ['uid'] = $data [0];
		$user ['username'] = $data [1];
		$user ['email'] = $data [3];
		$user ['api'] = UC_API;
		if ($user ['uid'] > 0) {
			$cookie = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/application.ini', 'cookie' );
			$time = time () + ($time ? $time : $cookie->time);
			setcookie ( 'user', Zend_Json::encode ( $user ), $time, $cookie->dir, $cookie->domain );
			uc_user_synlogin ( $user ['uid'] );
		}
		return $user;
	}

	/**
	 * 用户注册
	 * @return 大于 0:返回用户 ID，表示用户注册成功
	-1:用户名不合法
	-2:包含不允许注册的词语
	-3:用户名已经存在
	-4:Email 格式有误
	-5:Email 不允许注册
	-6:该 Email 已经被注册
	 */
	public function register($username , $password , $email) {

		return uc_user_register ( $username, $password, $email );
	}

	/**
	 * 退出
	 */
	public function logout() {

		$cookie = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/application.ini', 'cookie' );
		$time = time () - 1;
		setcookie ( 'user', NULL, $time, $cookie->dir, $cookie->domain );
		return uc_user_synlogout ();
	}

	/**
	 * 获取用户信息
	 * @return $email,$uid,$username
	 */
	public function getUserInfo($username , $isuid = 0) {

		return uc_get_user ( $username, $isuid );
	}

	/**
	 * 更新用户资料
	 * @return intval 	
	    1:更新成功
		0:没有做任何修改
		-1:旧密码不正确
		-4:Email 格式有误
		-5:Email 不允许注册
		-6:该 Email 已经被注册
		-7:没有做任何修改
		-8:该用户受保护无权限更改
	 */
	public function setUserInfo($username , $oldpw , $newpw , $email) {

		return uc_user_edit ( $username, $oldpw, $newpw, $email );
	}

	/**
	 * 删除用户
	 * @return intval 1:成功,0:失败
	 */
	public function delUser($uid) {

		return uc_user_delete ( $uid );
	}

	/**
	 * 检查email
	 * @return 1:成功,-4:Email 格式有误,-5:Email 不允许注册,-6:该 Email 已经被注册
	 */
	public function checkEmail($email) {

		return uc_user_checkemail ( $email );
	}

	/**
	 * 检查用户名
	 * @return 1:成功,-1:用户名不合法,-2:包含要允许注册的词语,-3:用户名已经存在
	 */
	public function checkUserName($username) {

		return uc_user_checkname ( $username );
	}

	/**
	 * 邮件队列
	 */
	public function sendEmail($uid , $email , $subject , $message) {

		return uc_mail_queue ( $uid, $email, $subject, $message );
	}
}
?>